package com.ln.controller.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ln.controller.BaseController;
import com.ln.dto.ParamsMap;
import com.ln.pojo.impl.NotesPOJO;
import com.ln.service.impl.NotesCollectService;
import com.ln.service.impl.NotesService;

@Controller
@RequestMapping("show")
public class ShowNoteController extends BaseController {
	@Autowired
	private NotesService notesService;
	@Autowired
	private NotesCollectService notesCollectService;

	@RequestMapping("code/{notesid}")
	public String newCode(HttpServletRequest request, Model model, @PathVariable("notesid") String notesid) {
		if (!notesid.matches("\\d*") || notesid.length() > 10) {
			return "redirect:/error/notpage";
		}
		try {
			NotesPOJO notesPOJO = notesService.selectOne(Integer
					.parseInt(notesid));
			
			int loginUserId = getLoginUserId(request);
			if (loginUserId > 0) {//查询当前Notes是否被收藏
				ParamsMap paramsMap = 
						ParamsMap.create(2)
						.add("notes_id", notesPOJO.getId())
						.add("user_id", loginUserId);
				if (notesCollectService.isExist(paramsMap)) {
					model.addAttribute("isExist", "Y");
				}
			}
			model.addAttribute("NOTES_OBJ", notesPOJO);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return "note/show-code-notes";
	}

}
